package com.caishi.lkx.handler;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.caishi.lkx.exam.vo.PkData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class PkDataListTypeHandler extends JacksonTypeHandler {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public PkDataListTypeHandler(Class<?> type) {
        super(type);
    }

    @Override
    public List<PkData> parse(String json) {
        try {
            if (json == null || json.isEmpty()) {
                return null;
            }
            return objectMapper.readValue(json, new TypeReference<List<PkData>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to parse JSON to List<PkData>", e);
        }
    }

    @Override
    public String toJson(Object obj) {
        try {
            if (obj == null) {
                return null;
            }
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to convert List<PkData> to JSON", e);
        }
    }
}
